//
//  YIMCompress.swift
//  AliyunOSSiOS
//
//  Created by ymac on 2024/8/5.
//

import Foundation
import AVFoundation
import FYVideoCompressor
import WXImageCompress

internal class PuwHWgrMwiOuoSTf: NSObject {
    func tKOBXqnxiKXdGeAJ(wIgXmVpmRMRncLHs: String, WlyMgrlTDrLWiMcu: (_ result: String?) -> Void) {
        func ywnUYsoXAfGPKYmt(heicPath: String) -> String? {
            let JZLWWusGZuqANRLp : UIImage? = UIImage(named:heicPath)
            if JZLWWusGZuqANRLp == nil {
              return nil
            }
            let eKrjXsynPPjCJFMZ = JZLWWusGZuqANRLp!.jpegData(compressionQuality: 1.0)
            let FCgEyiPNlUKLuHMC = NSTemporaryDirectory().appendingFormat("%d.jpg", Int(Date().timeIntervalSince1970 * 1000))
            FileManager.default.createFile(atPath: FCgEyiPNlUKLuHMC, contents: eKrjXsynPPjCJFMZ, attributes: nil)
            return FCgEyiPNlUKLuHMC
        }
        
        let VuFCApXqAiuIKaUF = ywnUYsoXAfGPKYmt(heicPath: wIgXmVpmRMRncLHs) ?? wIgXmVpmRMRncLHs
        let QzelNHgqucKxFLUE = UIImage(contentsOfFile: VuFCApXqAiuIKaUF)?.wxCompress()
        if QzelNHgqucKxFLUE == nil {
            WlyMgrlTDrLWiMcu(VuFCApXqAiuIKaUF)
            return
        }
        let kKsxeMWRvxOisRhl = jTrWSbasmlLQBHkN(QzelNHgqucKxFLUE: QzelNHgqucKxFLUE!)
        WlyMgrlTDrLWiMcu(kKsxeMWRvxOisRhl)
    }
    
    func bzetInzTlZDFtLvT(GGXyiTtwzKqMgeya: String, WlyMgrlTDrLWiMcu: @escaping (_ result: String?) -> Void) {
        FYVideoCompressor().compressVideo(URL(fileURLWithPath: GGXyiTtwzKqMgeya), quality: .highQuality) { kKsxeMWRvxOisRhl in
            switch kKsxeMWRvxOisRhl {
            case .success(let compressedVideoURL):
                WlyMgrlTDrLWiMcu(compressedVideoURL.absoluteString)
            case .failure(let error):
                WlyMgrlTDrLWiMcu(GGXyiTtwzKqMgeya)
            }
        }
    }
    
    func DDEhzNuCVDuCATRh(amwLsSsqearISEaM: String) -> String {
        let jyRCItYpjFORpNoa = URL(fileURLWithPath: amwLsSsqearISEaM)
        let lOTQjbxnoxrONagh = AVURLAsset(url: jyRCItYpjFORpNoa)
        let tNCPURtuqRrotrWa = AVAssetImageGenerator(asset: lOTQjbxnoxrONagh)
        tNCPURtuqRrotrWa.appliesPreferredTrackTransform = true
        let NhoLjQazYHvziqpY = CMTimeMakeWithSeconds(0.0, preferredTimescale: 1)
        let cefzlmsgRmPpHsMA = try! tNCPURtuqRrotrWa.copyCGImage(at: NhoLjQazYHvziqpY, actualTime: nil)
        let QzelNHgqucKxFLUE = UIImage(cgImage: cefzlmsgRmPpHsMA)
        return jTrWSbasmlLQBHkN(QzelNHgqucKxFLUE: QzelNHgqucKxFLUE)!
    }
    
    private func jTrWSbasmlLQBHkN(QzelNHgqucKxFLUE: UIImage) -> String? {
        guard let hpepJthyKYYxUCeV = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
        let amwLsSsqearISEaM = hpepJthyKYYxUCeV.appendingPathComponent("\(Date().timeIntervalSince1970).png")
        do {
            let pCLnZPapWvYHfsAu = QzelNHgqucKxFLUE.pngData()
            try pCLnZPapWvYHfsAu?.write(to: amwLsSsqearISEaM)
            return amwLsSsqearISEaM.path
        } catch {
            return nil
        }
    }
}
